package com.xidian.zhiancloud.controller.exam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.junit.runners.Parameterized.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.exam.ExamQuestionAnswer;
import com.xidian.zhiancloud.model.exam.ExamTypeCount;
import com.xidian.zhiancloud.model.exam.TExamPaper;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.exam.ITExamPaperService;


/**
 * 试卷Controller
 * 
 * @author zhouweitao
 * @date 2021-03-05
 */
@RestController
@RequestMapping("/paper")
public class TExamPaperController extends BaseController
{
    @Autowired
    private ITExamPaperService tExamPaperService;

    /**
     * 查询试卷列表
     * @param TExamPaper 传递参数
     * @return 返回列表
     */
    @GetMapping("/list")
    public ResultDTO list( TExamPaper tExamPaper)
    {
        Page<TExamPaper> pagedata = tExamPaperService.selectTExamPaperList(tExamPaper);
        Map<String, Object> data = new HashMap<String, Object>();
		data.put("datalist", pagedata);
		data.put("PageNum", pagedata.getPageNum()+1);
		data.put("PageSize", pagedata.getPageSize());
		data.put("PageTotal", pagedata.getPages());
		data.put("total", pagedata.getTotal());
		return ResultDTO.successOf(data,GeneralResponseEnums.SUCCESS);
    }


    /**
     * 获取试卷详细信息
     * @param  传递参数
     * @return ResultDTO
     */
    @GetMapping(value = "/{id}")
    public ResultDTO getInfo(@PathVariable("id") Long id)
    {
    	//ResultDTO.successOf(tExamPaperService.selectTExamPaperById(id), GeneralResponseEnums.SUCCESS);
        return ResultDTO.successOf(tExamPaperService.selectTExamPaperById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 新增试卷
     * @param  传递参数
     * @return ResultDTO
     */
    @PostMapping
    public ResultDTO add(@RequestBody TExamPaper tExamPaper)
    {
    	tExamPaperService.insertTExamPaper(tExamPaper);
    	return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 修改试卷
     * @param  传递参数
     * @return ResultDTO
     */
    @PutMapping
    public ResultDTO edit(@RequestBody TExamPaper tExamPaper)
    {
    	tExamPaperService.updateTExamPaper(tExamPaper);
    	return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除试卷
     * @param  传递参数
     * @return ResultDTO
     */
	@DeleteMapping("/{ids}")
    public ResultDTO remove(@PathVariable Long[] ids)
    {
		tExamPaperService.deleteTExamPaperByIds(ids);
		return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
	
	/**
     *  统计题目类型
     * @param  传递参数
     * @return ResultDTO
     */
	@GetMapping("/getTypeCount")
    public ResultDTO getTypeCount(BaseReqQuery query)
    {
		Page<ExamTypeCount> pagedata= tExamPaperService.getTypeCount(query);
		 Map<String, Object> data = new HashMap<String, Object>();
			data.put("datalist", pagedata);
			data.put("PageNum", pagedata.getPageNum()+1);
			data.put("PageSize", pagedata.getPageSize());
			data.put("PageTotal", pagedata.getPages());
			data.put("total", pagedata.getTotal());
			return ResultDTO.successOf(data,GeneralResponseEnums.SUCCESS);
    }
	
	
	@GetMapping("/getPaper")
    public ResultDTO getPaper(@Param("paperId")Long paperId, @Param("type")String type)
    {
		  return ResultDTO.successOf(tExamPaperService.getPaper(paperId,type), GeneralResponseEnums.SUCCESS);
    }
	
	 /**
     * 新增试卷答案
     * @param  传递参数
     * @return ResultDTO
     */
    @PostMapping("/addAnswers")
    public ResultDTO add(@RequestBody List<ExamQuestionAnswer> answers)
    {
    	Long id =getSessionUserid();
    	if(id==null) {
    		id=1L;
    	}
    	if(answers !=null && answers.size()>0) {
    		for (ExamQuestionAnswer examQuestionAnswer : answers) {
    			examQuestionAnswer.setUserId(id);
			}
    	}
    	tExamPaperService.insertTExamPaperAnswers(answers);
    	return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    /**
     * 获取已经程序的试卷，以及答案
     */
    @GetMapping("/getPaperAnswers")
    public ResultDTO getPaperAnswers(@Param("paperId")Long paperId,@Param("type")String type,@Param("userId")Long userId)
    {
		  return ResultDTO.successOf(tExamPaperService.getPaperAnswers(paperId,type,userId), GeneralResponseEnums.SUCCESS);
    }
}
